package cn.com.baiwei.service.impl;

import cn.com.baiwei.mapper.*;
import cn.com.baiwei.pojo.OrderForm;
import cn.com.baiwei.pojo.OrderFormParticulars;
import cn.com.baiwei.pojo.Vip;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.SimpleFormatter;

/**
 * (OrderForm)表服务实现类
 *
 * @author vv
 * @since 2021-09-17 10:22:07
 * @version 1.0
 */
@Service("orderFormService")
public class OrderFormServiceImpl {
    @Resource
    private OrderFormMapper orderFormMapper;

    @Resource
    private OrderFormParticularsMapper orderFormParticularsMapper;

    @Resource
    private FoodStuffMapper foodStuffMapper;

    @Resource
    private VipMapper vipMapper;

    @Resource
    private TableMapper tableMapper;

    /**
     * 根据模糊条件查询总个数
     *
     * @param name 查询条件
     * @return 返回查询到的总个数
     */
    public Map<String, Object> selectForCount(String name) {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.orderFormMapper.selectForCount(name));
        return map;
    }

    /**
     * 查询所有数据
     * @return  返回所有数据
     */
    public Map<String, Object> selectAll() {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.orderFormMapper.selectAll());
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    public Map<String, Object> selectOrderFormById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.orderFormMapper.selectOrderFormById(id));
        return map;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param state 支付状态
     * @return 实例对象
     */
    public Map<String, Object> selectOrderFormByState(Integer state) {
        Map<String, Object> map = new HashMap<>();
        // 前端端分离时，前端人员会首先判断code值是否满足200，如果不是200，则提醒用户失败
        map.put("code", 200);
        map.put("msg", "查询成功");
        map.put("data", this.orderFormMapper.selectOrderFormByState(state));
        return map;
    }

    /**
     * 查询分页数据
     *
     * @param index 查询起始位置
     * @param name  查询条件
     * @return 对象列表
     */
    public Map<String, Object> selectOrderFormForPage(int index, String name) {
    // 获取当前表中的总记录
        int tableCount = this.orderFormMapper.selectForCount(name);
        // 总页码计算   (总条数 - 1) / 每页显示条数  + 1
        // (100 - 1) / 10 + 1 = 10        (101 - 1) / 10 + 1 = 11      (99 - 1) / 10 + 1 = 10
        int pageCount = (tableCount - 1) / 10 + 1;
        // 计算每页开始的下标值
        index = (index - 1) * 10;
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("pageCount", pageCount);  // 查询的记录总页码
        map.put("count", tableCount);     // 当前表中的总条数
        map.put("data", this.orderFormMapper.selectOrderFormForPage(index, name));
        return map;
    }

    /**
     * 新增数据
     *
     * @param orderForm 实例对象
     * @return 实例对象
     */
    public Map<String, Object> insertOrderForm(OrderForm orderForm) {
        orderForm.setDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        orderForm.setOrderDate(new Date());//生成订单时间
        this.orderFormMapper.insertOrderForm(orderForm);//将订单id注入订单对象中
        List<OrderFormParticulars> list = orderForm.getOrderFormParticulars();//获得订单详情集合
        Integer id = orderForm.getId();
        for (OrderFormParticulars orderFormParticulars:list) {
            orderFormParticulars.setOrderFormId(id);
        }
        orderFormParticularsMapper.insert(list);//增加订单详情
        foodStuffMapper.updateCutByIds(list);//减少菜品库存数量
        tableMapper.updateStateById(1,orderForm.getTableId());//更改桌位状态为已占

        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "新增订单成功");
        map.put("data", id);
        return map;
    }

    /**
     * 通过ID修改单条数据
     *
     * @param orderForm 实例对象
     * @return 实例对象
     */
    public Map<String, Object> updateOrderFormById(OrderForm orderForm) {
        this.orderFormMapper.updateOrderFormById(orderForm);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "更新成功");
        return map;
    }

    /**
     * 通过订单id修改支付状态(结账)
     *
     * @param orderForm 订单对象
     * @return 实例对象
     */
    public Map<String, Object> updateStateById(OrderForm orderForm) {
        OrderForm orderForm1 = orderFormMapper.selectOrderFormById(orderForm.getId());
        String ordMoney = orderForm1.getOrderMoney();
        String vipPhone = orderForm.getVipPhone();
        if(vipPhone !=null) {  // 判断是否有手机号
            Vip vip = vipMapper.selectByPhone(vipPhone); //有,则查找
            if (vip == null) {  // 判断是否是会员
                Vip vipF = new Vip();
                vipF.setVipPhone(vipPhone);
                vipF.setVipTime(new Date());
                vipF.setVipIntegral(Integer.parseInt(ordMoney));
                vipMapper.insert(vipF);
            }
            orderForm.setOrderMoney(ordMoney);
            vipMapper.updateAddByOFPhone(orderForm); //添加会员积分
        }
        this.orderFormMapper.updateStateById(orderForm);
        tableMapper.updateStateById(0,orderForm.getTableId());
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "更新成功");
        return map;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    public Map<String, Object> deleteOrderFormById(String id) {
        this.orderFormMapper.deleteOrderFormById(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除成功");
        return map;
    }
}